# Using CentOS7
FROM centos:centos7
# Author for this Dockerfile
MAINTAINER Charkey "charkeyqk@gmail.com"

# install necessary softwares
RUN yum install -y openssh openssh-server openssh-clients httpd wget unzip git
# create a directory for sshd
RUN mkdir /var/run/sshd
# generate key pair
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
# change password for root
RUN /bin/echo 'root:charkey' |chpasswd
# add a new user
RUN useradd charkey
# change password for new user
RUN /bin/echo 'charkey:charkey@fzu' |chpasswd
# SSH login fix. Otherwise user is kicked off after login
RUN /bin/sed -i 's/.*session.*required.*pam_loginuid.so.*/session optional pam_loginuid.so/g' /etc/pam.d/sshd
# language setting
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" > /etc/default/local

# download and untar jdk8
RUN wget -P /root --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64.tar.gz"
RUN tar -zxvf /root/jdk-8u60-linux-x64.tar.gz -C /root
# download and untar tomcat7
RUN wget -P /root https://archive.apache.org/dist/tomcat/tomcat-7/v7.0.61/bin/apache-tomcat-7.0.61.tar.gz
RUN tar -zxvf /root/apache-tomcat-7.0.61.tar.gz -C /root
# change port from 8080 to 80
RUN sed -i "s/port=\"8080\"/port=\"80\" URIEncoding=\"UTF-8\" compression=\"on\" compressionMinSize=\"2048\" noCompressionUserAgents=\"gozilla,traviata\" compressableMimeType=\"text\/html,text\/xml,text\/javascript,application\/javascript,text\/css,text\/plain\"/g" `grep 'port="8080"' -rl /root/apache-tomcat-7.0.61/conf/server.xml`
# download and untar maven3
RUN wget -P /root http://www.eu.apache.org/dist/maven/maven-3/3.3.3/binaries/apache-maven-3.3.3-bin.tar.gz
RUN tar -zxvf /root/apache-maven-3.3.3-bin.tar.gz -C /root
# create local maven repo
RUN mkdir -p /root/apache-maven-repo/cn
# change settings of the local maven repo
RUN sed -i "s/<\!-- interactiveMode/<localRepository>\/root\/apache-maven-repo<\/localRepository><\!-- interactiveMode/g" `grep '<!-- interactiveMode' -rl /root/apache-maven-3.3.3/conf/settings.xml`
# install database: MariaDB
RUN yum -y install mariadb-server mariadb
RUN mysql_install_db --user=mysql --ldata=/var/lib/mysql/

# Settings for Environment Variables
ENV JAVA_HOME /root/jdk1.8.0_60
ENV PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
ENV CLASSPATH .:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tool.jar
ENV CATALINA_HOME /root/apache-tomcat-7.0.61
ENV M2_HOME /root/apache-maven-3.3.3
ENV PATH=$PATH:$M2_HOME/bin
ENV M2_REPO /root/apache-maven-repo

RUN echo export JAVA_HOME=$JAVA_HOME >> /etc/profile
RUN echo export PATH=$PATH >> /etc/profile
RUN echo export CLASSPATH=$CLASSPATH >> /etc/profile
RUN echo export CATALINA_HOME=$CATALINA_HOME >> /etc/profile
RUN echo export M2_HOME=$M2_HOME >> /etc/profile

# create script for checkout project code and finish the first time compile
RUN echo 'git clone https://deploy:deployme@git.oschina.net/charkey/charkey-proj.git /root/charkey-proj' > /root/checkout-code.sh
RUN echo rm -rf $M2_REPO/cn/charkey-proj >> /root/checkout-code.sh
RUN echo unzip /root/charkey-proj/lib/charkey-proj-libs-v1.0.zip -d /root/apache-maven-repo/cn >> /root/checkout-code.sh
RUN chmod +x /root/checkout-code.sh

# run compile & package
#RUN git clone https://deploy:deployme@git.oschina.net/charkey/charkey-proj.git /root/charkey-proj
#RUN unzip /root/charkey-proj/lib/charkey-proj-libs-v1.0.zip -d /root/apache-maven-repo/cn
#RUN mvn compile package -Dmaven.test.skip=true -f /root/charkey-proj/pom.xml

# Expose the necessary ports
EXPOSE 22 80 3306

#ENTRYPOINT mysqld_safe --user=mysql --datadir='/var/lib/mysql/' --nowatch && /root/apache-tomcat-7.0.61/bin/catalina.sh start && /usr/sbin/sshd -D
ENTRYPOINT /usr/sbin/sshd -D